import {Component, OnInit} from '@angular/core';
import {AddGooInventoryComponent} from '../add-goo-inventory.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {GooInventory} from '../../goo-inventory.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {FroalaEditor} from '../../../../../frame/global/froala-editor';
import {GooGoodsCondition} from '../../../goo-goods/goo-goods.type';
import {isNullOrUndefined} from 'util';
import {Zero3Option} from '../../../../../frame/enum/zero3-option';
import {DraFreeLotteryCondition} from '../../../../draw-management/dra-free-lottery/dra-free-lottery.type';
import {DraIntegralLotteryCondition} from '../../../../draw-management/dra-integral-lottery/dra-integral-lottery.type';

@Component({
    selector: 'app-main-add-goo-inventory',
    templateUrl: './main-add-goo-inventory.component.html',
    styleUrls: ['./main-add-goo-inventory.component.css']
})
export class MainAddGooInventoryComponent implements OnInit {

    froalaEditorOptions = Object.assign(FroalaEditor.options, {});

    errRes = new ErrRes();

    domain = new GooInventory();

    extend = {
        domain: {
            linkedGoods: {
                items: [],
                itemsCash: [], // 现金商品
                itemsIntegral: [], // 积分商品
                itemsDrawFree: [], // 免费抽奖商品
                itemsDrawIntegral: [], // 积分抽奖商品
                nickname: '关联对象',
                change: (event) => {
                    console.log(event);
                    const itemo = this.extend.domain.linkedGoods.items.find(item => item.id === event.id);
                    if (!isNullOrUndefined(itemo)) {
                        this.domain.goodsName = itemo.name;
                    }
                }
            },
            enumGoodsType: {
                items: Zero3Option.GOODS_TYPE,
                nickname: '关联类型',
                initItems: () => {
                    if (isNullOrUndefined(this.domain)) {
                        return null;
                    }
                    console.log(this.domain.enumGoodsType);
                    switch (this.domain.enumGoodsType) {
                        case Zero3Enum.GOODS_TYPE_CASH:
                            this.extend.domain.linkedGoods.items
                                = this.extend.domain.linkedGoods.itemsCash;
                            console.log('this.extend.domain.linkedGoods.itemsCash');
                            break;
                        case Zero3Enum.GOODS_TYPE_INTEGRAL:
                            this.extend.domain.linkedGoods.items
                                = this.extend.domain.linkedGoods.itemsIntegral;
                            console.log('this.extend.domain.linkedGoods.itemsIntegral');
                            break;
                        case Zero3Enum.GOODS_TYPE_DRAW_FREE:
                            this.extend.domain.linkedGoods.items
                                = this.extend.domain.linkedGoods.itemsDrawFree;
                            console.log('this.extend.domain.linkedGoods.itemsDrawFree');
                            break;
                        case Zero3Enum.GOODS_TYPE_DRAW_INTEGRAL:
                            this.extend.domain.linkedGoods.items
                                = this.extend.domain.linkedGoods.itemsDrawIntegral;
                            console.log('this.extend.domain.linkedGoods.itemsDrawIntegral');
                            break;
                        default:
                            this.extend.domain.linkedGoods.items = [];
                            console.log('this.extend.domain.linkedGoods.null');
                    }
                    console.log(this.extend.domain.linkedGoods.items);
                },
                change: (event) => {
                    this.extend.domain.enumGoodsType.initItems();
                    // re init domain fkGoods
                    this.domain.fkGoods = null;
                    this.domain.goodsName = null;
                }
            },
            goods: {
                items: [],
                nickname: '关联商品',
                change: (event) => {
                }
            },
            default: () => {
                this.domain.enumGoodsType = null;
                this.domain.fkGoods = null;
                this.domain.serAudit = Zero3Enum.AUDIT_FLAG_WAIT_SUBMIT;
                this.domain.serUsing = Zero3Enum.ENABLE;
                this.extend.domain.serAudit.nickname = Zero3Enum.AUDIT_FLAG.get(this.domain.serAudit);
            },
            serAudit: {
                nickname: ''
            },
            file: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.file.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.file.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                        } else {
                            this.extend.domain.file.oss.fileUrl = '';
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            serInputGroupData: {
                icon: fa.faCalendar,
                nickname: '日期',
                value: () => {
                    return this.domain.serInputGroupData;
                },
                focus: (event) => {
                    this.domain.serInputGroupData = event.target.value.toString();
                },
                blur: (event) => {
                }
            },
        },
    };

    func = {
        loadAllAfterInit: () => {
            this.extend.domain.enumGoodsType.initItems();
        },
        save: {
            req: (event) => {
                Services.gooInventory.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.gooInventory.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public addGooInventory: AddGooInventoryComponent) {
        this.addGooInventory.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
        // 现金商品
        Services.gooGoods.list(new GooGoodsCondition({
            serGoodsIsactive: Zero3Enum.ITEM_NOSEND
        }), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.linkedGoods.itemsCash = [];
                item.page.content.forEach(item2 => {
                    this.extend.domain.linkedGoods.itemsCash.push({
                        id: item2.uuid, name: item2.serGoodsName, headImg: item2.serGoodsP2, price: item2.serGoodsPrice
                    });
                });
            }
            this.func.loadAllAfterInit();
        });
        // 积分商品
        Services.gooGoods.list(new GooGoodsCondition({
            serGoodsIsactive: Zero3Enum.ITEM_YESSEND
        }), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.linkedGoods.itemsIntegral = [];
                item.page.content.forEach(item2 => {
                    this.extend.domain.linkedGoods.itemsIntegral.push({
                        id: item2.uuid, name: item2.serGoodsName, headImg: item2.serGoodsP2, price: item2.serGoodsPrice
                    });
                });
            }
            this.func.loadAllAfterInit();
        });
        // 免费抽奖商品
        Services.draFreeLottery.list(new DraFreeLotteryCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.linkedGoods.itemsDrawFree = [];
                item.page.content.forEach(item2 => {
                    this.extend.domain.linkedGoods.itemsDrawFree.push({
                        id: item2.uuid, name: item2.serGoodsDrawPname, headImg: item2.serGoodsDrawP2, price: item2.serGoodsDrawPrice
                    });
                });
            }
            this.func.loadAllAfterInit();
        });
        // 积分抽奖商品
        Services.draIntegralLottery.list(new DraIntegralLotteryCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.linkedGoods.itemsDrawIntegral = [];
                item.page.content.forEach(item2 => {
                    this.extend.domain.linkedGoods.itemsDrawIntegral.push({
                        id: item2.uuid, name: item2.serGoodsDrawName, headImg: item2.serGoodsDrawP2, price: item2.serGoodsDrawPrice
                    });
                });
            }
            this.func.loadAllAfterInit();
        });
    }


}
